package com.leyou.page.service.impl;

import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.service.GoodsPageService;
import com.leyou.page.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class GoodsPageServiceImpl implements GoodsPageService {
    @Autowired
    private ItemClient itemClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String SPU_KEY_PREFIX = "page:spu:id:";
    private static final String SKU_KEY_PREFIX = "page:sku:id:";
    private static final String DETAIL_KEY_PREFIX = "page:detail:id:";
    private static final String CATEGORY_KEY_PREFIX = "page:category:id:";
    private static final String BRAND_KEY_PREFIX = "page:brand:id:";
    private static final String SPEC_KEY_PREFIX = "page:spec:id:";

    @Override
    public String querySpuById(Long spuId) {
        //远程查询数据
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        //将查询到的数据转为vo
        SpuVO spuVO = spuDTO.toEntity(SpuVO.class);
        spuVO.setCategoryIds(spuDTO.getCategoryIds());
        //把vo转为json
        String json = JsonUtils.toJson(spuVO);
        //写入redis
        stringRedisTemplate.opsForValue().set(SPU_KEY_PREFIX+spuId,json);
        //返回
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        //远程查询数据
        List<SkuDTO> skuDTOList = itemClient.querySkuBySpuId(spuId);
        //把vo转为json
        String json = JsonUtils.toJson(skuDTOList);
        //写入redis
        stringRedisTemplate.opsForValue().set(SKU_KEY_PREFIX+spuId,json);
        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        //远程查询数据
        SpuDetailDTO spuDetailDTO = itemClient.querySpuDetailById(spuId);
        //把vo转为json
        String json = JsonUtils.toJson(spuDetailDTO);
        //写入redis
        stringRedisTemplate.opsForValue().set(DETAIL_KEY_PREFIX+spuId,json);
        return json;
    }

    //查询商品分类数据
    @Override
    public String loadCategoriesData(List<Long> ids) {
        //查询category信息
        List<CategoryDTO> categoryDTOList = itemClient.queryCategoryByIds(ids);
        if(categoryDTOList==null||categoryDTOList.size()<=0){
            throw  new LyException(404,"该商品不存在！");
        }
        //转换vo
        List<CategoryVO> categoryVOList = categoryDTOList.stream()
                .map(categoryDTO -> categoryDTO.toEntity(CategoryVO.class))
                .collect(Collectors.toList());
        //序列化为json
        String json = JsonUtils.toJson(categoryVOList);

        //写入redis
        stringRedisTemplate.opsForValue().set(CATEGORY_KEY_PREFIX+ids.get(2),json);
        //返回
        return json;
    }

    @Override
    public String loadBrandData(Long id) {
        //远程查询数据
        BrandDTO brandDto = itemClient.findBrandById(id);
        //将查询数据转为vo
        BrandVO brandVO = brandDto.toEntity(BrandVO.class);
        //把vo转为json
        String json = JsonUtils.toJson(brandVO);
        //写入redis
        stringRedisTemplate.opsForValue().set(BRAND_KEY_PREFIX+id,json);
        //返回
        return json;
    }

    // 查询规格数据
    @Override
    public String loadSpecData(Long categoryId) {
        //根据分类id查询规格组及组内参数
        List<SpecGroupDTO> specGroups = itemClient.querySpecList(categoryId);
        if(specGroups==null ||specGroups.size()<=0){
            throw new LyException(404,"该商品不存在啦！");
        }
        //转换vo 因为这个结果有两个需要转vo，所以只有先转外面的vo在转里面的vo
        List<SpecGroupVO> groupVOList = new ArrayList<>(specGroups.size());
        for (SpecGroupDTO specGroup : specGroups) {
            //创建vo
            SpecGroupVO groupVO = new SpecGroupVO();
            //放入集合
            groupVOList.add(groupVO);
            //填写属性
            //name属性
            groupVO.setName(specGroup.getName());
            //params属性
            List<SpecParamVO> params = specGroup.getParams().stream()
                    .map(paramDTO -> paramDTO.toEntity(SpecParamVO.class))
                    .collect(Collectors.toList());
            groupVO.setParams(params);
        }
        //序列化
        String json = JsonUtils.toJson(groupVOList);
        //写入redis
        stringRedisTemplate.opsForValue().set(SPEC_KEY_PREFIX+categoryId,json);
        //返回结果
        return json;
    }

    @Override
    public void deleteSku(Long spuId) {
        stringRedisTemplate.delete(SKU_KEY_PREFIX+spuId);
    }
}
